from PySide6.QtWidgets import QApplication,QMainWindow
import json
import pandas as pd
import requests

# class MainUI(QMainWindow,Ui_MainWindow):
#     def __init__(self,parent=None):
#         super(MainUI,self).__init__(parent)
#         self.setupUi(self)


#获取开盘啦龙虎榜数据
def get_data():
    url = 'https://applhb.longhuvip.com/w1/api/index.php'
    headers = {
        'user-agent':'Mozilla/5.0(Linux; Android 7.1.2; SM-G955N Build/NRD90M.G955NKSU1AQDC; wv)'
    }
    # POST请求参数
    params = {
        'st': '500',
        'Index': '0',
        'c': 'LongHuBang',
        'PhoneOSNew': 1,
        'a': 'GetStockList',
        'DeviceID': '0f6ac4ae-370d-3091-a618-1d9dbb2ecce0',
        'apiv': 'w31',
        'Type': 2,
        'UserID': 0,
        'Token': 0,
        'Time': 0,
    }
    # 发送POST请求
    response = requests.post(url, params=params, headers=headers, verify=False)
    # 将编码设置为当前编码
    response.encoding = response.apparent_encoding
    # 解析JSON数据
    data = json.loads(response.text)
    print(data)
    # 获取买入营业部、卖出营业部和风口概念等数据
    BIcon = data.get('BIcon')
    SIcon = data.get('SIcon')
    fkgn = data.get('fkgn')
    lb = data.get('lb')
    all_data = pd.DataFrame()
    # 遍历股票列表，提取数据
    for item in data.get('list'):
        ID = item.get('ID')
        item_data = [
            ID,
            item.get('Name'),
            item.get('IncreaseAmount'),
            item.get('BuyIn'),
            item.get('JoinNum'),
            ','.join(BIcon.get(ID, [])),
            ','.join(SIcon.get(ID, [])),
            ','.join(fkgn.get(ID, {}).values()),
            lb.get(ID),
        ]

        # 将数据转换成DataFrame类型
        data = pd.DataFrame(item_data).T
        dict = {0:'股票代码', 1:'股票名称', 2:'涨幅', 3:'净买入', 4:'关联数', 5:'买入营业部', 6:'卖出营业部', 7:'风口概念', 8:'连板数'}
        data.rename(columns=dict, inplace=True)
        all_data = all_data._append(data, ignore_index=True)
    # 返回DataFrame类型数据
    return all_data

  # ['881122', '光学光电子', 4.572]
def get_data1():

    #https://apphq.longhuvip.com/w1/api/index.php?apiv=w30&PhoneOSNew=1&VerSion=5.6.0.2
    url = 'https://apphq.longhuvip.com/w1/api/index.php'
    headers = {
        'user-agent':'Mozilla/5.0(Linux; Android 7.1.2; SM-G955N Build/NRD90M.G955NKSU1AQDC; wv)'
    }
    #a=GetStockIDPlate&apiv=w30&Type=2&c=StockL2Data&StockID=002952&PhoneOSNew=1&UserID=0&DeviceID=00000000-2de4-c43b-ffff-ffff99d603a9&Token=0&
    # POST请求参数
    params = {
        'st': '500',
        'Index': '0',
        'c': 'StockL2Data',
        'PhoneOSNew': 1,
        'a': 'GetStockIDPlate',
        'DeviceID': '00000000-2de4-c43b-ffff-ffff99d603a9',
        'apiv': 'w31',
        'Type': 2,
        'UserID': 0,
        'Token': 0,
        'Time': 0,
        'StockID': '002952',
    }
    #去除证书警告
    requests.packages.urllib3.disable_warnings()
    # 发送POST请求
    response = requests.post(url, params=params, headers=headers, verify=False)
    # 将编码设置为当前编码
    response.encoding = response.apparent_encoding
    # 解析JSON数据
    data = json.loads(response.text)
    # 返回DataFrame类型数据
    return data


#{'BaceFaceList': [['Mini/Micro', '5.06', 801434], ['VR/AR/MR', '4.04', 801169], ['北交所', '3.68', 801595], ['烟花爆竹', '2.61', 801825]], 'Day': '2023-12-29', 'Time': 1703919794, 'ttag': 0.0006279999999999619, 'errcode': '0'}
def get_data2():

    #https://apphq.longhuvip.com/w1/api/index.php?apiv=w31&PhoneOSNew=1&VerSion=5.7.0.14
    url = 'https://apphq.longhuvip.com/w1/api/index.php?apiv=w31&PhoneOSNew=1&VerSion=5.7.0.14'
    headers = {
        'user-agent':'Mozilla/5.0(Linux; Android 7.1.2; SM-G955N Build/NRD90M.G955NKSU1AQDC; wv)'
    }
    #c=Index&a=GetInfo&View=2%2C3%2C4%2C5%2C7%2C8%2C9%2C10&UserID=65996&Token=ba06ae2cbf8577b6e8a0735ab2d1f68c
    # POST请求参数
    params = {
        'c': 'Index',
        'a': 'GetInfo',
        'UserID': '65996',
        'Token': 'ba06ae2cbf8577b6e8a0735ab2d1f68c',
        'View': '2%2C3%2C4%2C5%2C7%2C8%2C9%2C10',
    }
    #去除证书警告
    requests.packages.urllib3.disable_warnings()
    # 发送POST请求
    response = requests.post(url, params=params, headers=headers, verify=False)
    # 将编码设置为当前编码
    response.encoding = response.apparent_encoding
    # 解析JSON数据
    data = json.loads(response.text)
    # 返回DataFrame类型数据
    return data


def get_data3():

    #https://apphq.longhuvip.com/w1/api/index.php?apiv=w30&PhoneOSNew=1&VerSion=5.6.0.2
    url = 'https://apphq.longhuvip.com/w1/api/index.php?apiv=w30&PhoneOSNew=1&VerSion=5.6.0.2 '
    headers = {
        'user-agent':'Mozilla/5.0(Linux; Android 7.1.2; SM-G955N Build/NRD90M.G955NKSU1AQDC; wv)'
    }
    #a=GetStockIDPlate&apiv=w30&Type=2&c=StockL2Data&StockID=002578&PhoneOSNew=1&UserID=0&DeviceID=00000000-2de4-c43b-ffff-ffff99d603a9&Token=0&
    # POST请求参数
    params = {
        'a': 'GetStockIDPlate',
        'apiv': 'w30',
        'Type': '2',
        'c': 'StockL2Data',
        'StockID': '002578',
        'PhoneOSNew': '1',
        'UserID': 0,
        'DeviceID': '00000000-2de4-c43b-ffff-ffff99d603a9',
        'Token': 0,

    }
    #去除证书警告
    requests.packages.urllib3.disable_warnings()
    # 发送POST请求
    response = requests.post(url, params=params, headers=headers, verify=False)
    # 将编码设置为当前编码
    response.encoding = response.apparent_encoding
    # 解析JSON数据
    data = json.loads(response.text)
    # 返回DataFrame类型数据
    return data
# Press the green button in the gutter to run the script.

def get_data2_1():

    #https://apphq.longhuvip.com/w1/api/index.php?apiv=w31&PhoneOSNew=1&VerSion=5.7.0.14
    url1 = "https://apphq.longhuvip.com/w1/api/index.php"
    hd = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Connection": "Keep-Alive", "Host": "",
          "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 5.1.1; SM-G977N Build/LMY48Z)", "Accept-Encoding": "gzip"}
    hd["Host"] = "apphq.longhuvip.com"
    d1 = {"Order": "1", "a": "RealRankingInfo", "st": "30", "apiv": "w36","Type": "1","c":"ZhiShuRanking", "PhoneOSNew": "1", "DeviceID": "3c18c58d-8617-3b2b-9fb3-5ac2f73394a9",
        "VerSion": "5.14.0.0", "ZSType": "7","Index":""}
    d1["Index"] = str(60)
    #去除证书警告
    requests.packages.urllib3.disable_warnings()
    # 发送POST请求
    response = requests.post(url1, headers=hd, data=d1, verify=False)
    # 将编码设置为当前编码
    response.encoding = response.apparent_encoding
    # 解析JSON数据
    print(response.text)
    data = json.loads(response.text)
    # 返回DataFrame类型数据
    return data

if __name__ == '__main__':
    df = get_data2_1()
    #df.to_csv('longhubang.csv', encoding='gbk')
    print(df)


#https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=RealRankingInfo&st=21&apiv=w29&Type=1&c=ZhiShuRanking&PhoneOSNew=1&DeviceID=ffffffff-cf63-0494-0000-00004e79e45a&Index=0&ZSType=7&
'''
 [
            "801169",
            "VR/AR/MR",
            10698,
            4.043,
            0.336,
            90809073082,
            2293200051,
            9278707778,
            -6985507727,
            1.333,
            1913229188864,
            4.15,
            1410574231,
            2267123554304,
            78200887,
            38.1286,
            25.6113,
            10698,
            4.043
        ]
        "Title": [
        "第三季度机构增仓",
        "2023年平均PE",
        "2024年平均PE"
    ]
'''

'''
https://apphq.longhuvip.com/w1/api/index.php?st=50&Index=0&PidType=5&Order=1&PhoneOSNew=1&a=DailyLimitPerformance2&apiv=w30&Type=5&c=HomeDingPan&DeviceID=00000000-2de4-c43b-ffff-ffff99d603a9&
 [
                "002395",
                "双象股份",
                0,
                "",
                20.14,
                1,
                "VR/AR/MR、消费电子",
                -125699357,
                288766652,
                -414466009,
                1528116545,
                1914794184,
                77.64,
                4,
                18.15,
                "昨4连板",
                0
            ]
'''

'''
https://apphq.longhuvip.com/w1/api/index.php?Order=0&a=ZhiShuStockList_W8&st=60&c=ZhiShuRanking&PhoneOSNew=1&old=1&DeviceID=00000000-2de4-c43b-ffff-ffff99d603a9&IsZZ=0&Token=0&Index=0&apiv=w29&Type=6&UserID=0&PlateID=801220&
https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=ZhiShuStockList_W8&st=75&c=ZhiShuRanking&PhoneOSNew=1&old=1&DeviceID=00000000-2de4-c43b-ffff-ffff99d603a9&IsZZ=0&Token=0&Index=0&apiv=w29&Type=20&UserID=0&PlateID=801220&
https://apphq.longhuvip.com/w1/api/index.php?Order=0&a=ZhiShuStockList_W8&st=60&c=ZhiShuRanking&PhoneOSNew=1&old=1&DeviceID=00000000-2de4-c43b-ffff-ffff99d603a9&IsZZ=0&Token=0&Index=0&apiv=w29&Type=6&UserID=0&PlateID=801067&

涨停
https://apphq.longhuvip.com/w1/api/index.php?a=GetPlateInfo&st=50&apiv=w29&c=DailyLimitResumption&PhoneOSNew=1&DeviceID=00000000-2de4-c43b-ffff-ffff99d603a9&Index=0&

#炸板
https://apphq.longhuvip.com/w1/api/index.php?st=30&Is_st=1&FilterGem=0&Index=0&PidType=2&FilterMotherboard=0&Order=1&PhoneOSNew=1&FilterTIB=0&a=DaBanList&apiv=w34&Type=4&c=HomeDingPan&

#竞价
https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=DaBanList&st=60&c=HomeDingPan&PhoneOSNew=1&Index=0&Is_st=1&PidType=8&apiv=w34&Type=18&FilterMotherboard=0&Filter=0&FilterTIB=0&

#跌停
https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=DaBanList&st=60&c=HomeDingPan&PhoneOSNew=1&&Index=0&Is_st=1&PidType=3&apiv=w34&Type=6&FilterMotherboard=0&Filter=0&FilterTIB=0&FilterGem=0&

#人气排行
https://apphq.longhuvip.com/w1/api/index.php?st=100&a=GetHotPHB&UserID=0&apiv=w29&Type=13&Index=0&c=StockBidYiDong&Order=1&PhoneOSNew=1&DeviceID=ffffffff-cf63-0494-0000-00004e79e45a&Token=0&

#板块
https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=RealRankingInfo&st=21&apiv=w29&Type=1&c=ZhiShuRanking&PhoneOSNew=1&DeviceID=ffffffff-cf63-0494-0000-00004e79e45a&Index=0&ZSType=7&
#板块下个股
https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=InterviewsByDate&st=51&c=ZhiShuRanking&PhoneOSNew=1&DeviceID=00000000-2de4-c43b-ffff-ffff99d603a9&DEnd=2023-12-29&Token=0&Index=0&DStart=2023-12-28&apiv=w28&Type=2&UserID=0&PlateID=801212&


'''